package com.wen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.pojo.StockDetails;

import com.wen.pojo.dto.GatherDetailsDto;
import com.wen.pojo.vo.StockDetailsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface StockDetailsMapper extends BaseMapper<StockDetails> {

    Page<StockDetails> pageListStockDetails(Page page, @Param("stockDetailsVo") StockDetailsVo stockDetailsVo);

    @Select("SELECT gd.*,g.stock_id FROM `gather` g,`gather_details` gd,`product` p,`supplier` s WHERE gd.gather_id=g.gather_id AND gd.product_id=p.`id` AND gd.supplier_id=s.supplier_id AND g.gather_order_number=#{id}")
    List<GatherDetailsDto> GatherDetailsById(String id);

    /**
     * 通过申请的修改
     * @param id
     * @return
     */
    @Update("UPDATE gather t1 INNER JOIN purchase_order t2 ON t1.purchase_id = t2.purchase_id INNER JOIN `purchase_order_details` t3 ON t2.purchase_id = t3.purchase_id SET t1.check_tag='s001-3',t2.gather_tag='k001-3',t3.gather_tag='k001-3' WHERE t1.gather_order_number=#{id}")
    boolean UpStockIds(String id);

    /**
     * 根据id查询看是否有相同的
     */
    @Select("select sd.* FROM stock_details sd,stock s,product p,supplier sp WHERE sd.stock_id=s.stock_id AND sd.product_id=p.`id` AND sd.supplier_id=sp.`supplier_id` AND sd.stock_id=#{stockId} AND sd.product_id=#{productId} AND sd.supplier_id=#{supplierId}")
    StockDetails stockId(@Param("stockId") Long  stockId,@Param("productId")long productId,@Param("supplierId")long supplierId);

    /**
     * 根据id修改数量两个字段
     */
    @Update("UPDATE `stock_details` SET amount=amount+#{amount1} ,real_amount=real_amount+#{realAmount} WHERE id=#{ids}")
    boolean UpStockId(@Param("amount1") Long  amount1,@Param("realAmount") Long  realAmount,@Param("ids") Long  ids);

    @Update("UPDATE stock_details SET `real_amount` = `real_amount` -   #{count} WHERE `product_id` = #{pid}")
    int  UpdateCount  (@Param("count") Integer count, @Param("pid") String pid);

}
